package com.kuka.bpm.controller;

import com.kuka.bpm.model.TaskInfo;
import io.swagger.annotations.Api;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Model;
import org.flowable.task.api.Task;
import org.flowable.ui.common.model.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(value="流程相关接口")
public class BpmController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ProcessEngine processEngine;


    @RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json")
    public UserRepresentation getAccount() {
        UserRepresentation userRepresentation = new UserRepresentation();
        userRepresentation.setId("admin");
        userRepresentation.setEmail("admin@flowable.org");
        userRepresentation.setFullName("Admin");
        userRepresentation.setLastName("Admin");
        userRepresentation.setFirstName("Test");
        List<String> privileges = new ArrayList<>();
        privileges.add("access-idm");
        privileges.add("access-rest-api");
        privileges.add("access-task");
        privileges.add("access-modeler");
        privileges.add("access-admin");
        userRepresentation.setPrivileges(privileges);
        return userRepresentation;
    }

    @RequestMapping(value="/deployProcess", method= RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String deployProcess() {
        repositoryService.createDeployment().addClasspathResource("qingjia.bpmn20.xml").deploy();
        return null;
    }

    @RequestMapping(value="/startProcess", method= RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String startProcessInstance(@RequestParam String processKey) {
        runtimeService.startProcessInstanceByKey(processKey);
        return null;
    }

    @RequestMapping(value="/getTasksByAssign", method= RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<TaskInfo> getTasksByAssign(@RequestParam String assignee) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
        //List<Task> tasks = taskService.createTaskQuery().taskCandidateUser("kermit").list();
        List<TaskInfo> dtos = new ArrayList<TaskInfo>();
        for (Task task : tasks) {
            dtos.add(new TaskInfo(task.getId(), task.getName()));
        }
        return dtos;
    }

    @RequestMapping(value="/assignTask", method= RequestMethod.POST, produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String assignTask(@RequestParam String taskId,@RequestParam String name) {
        taskService.claim(taskId, name);//领取任务，或者分配任务 设置后不能再调用
        //taskService.setAssignee(taskId, name); 任务转交给谁处理
        return null;
    }
    @RequestMapping(value="/completeTask", method= RequestMethod.POST,produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String completeTask(@RequestParam String taskId) {
        taskService.complete(taskId);
        return null;
    }
    @RequestMapping(value="/historyById", method= RequestMethod.POST,produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String historyById(@RequestParam String processId) {
        HistoryService historyService = processEngine.getHistoryService();
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).singleResult();
        System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());
        return null;
    }

//    @RequestMapping(value="/taskinfo", method= RequestMethod.GET)
//    public void taskinfo(@RequestParam String modelKey) {
//        Model model = repositoryService.createModelQuery().modelKey(modelKey).singleResult();
//        System.out.println(model.getName());
//    }


}